﻿Imports System.IO
Imports System.Net

Public Class gaysComm

    ''' <summary>
    ''' Führt simples GET-Request gegen eine URL aus und gibt Response direkt zurück
    ''' </summary>
    ''' <param name="Url">URL die aufgerufen werden soll</param>
    ''' <returns>HTTP Response</returns>
    ''' <remarks></remarks>
    Private Function simpleHttpGetRequest(ByVal Url As String) As String
        Try
            Dim request As WebRequest = WebRequest.Create(Url)
            request.Method = "GET"
            Dim dataStream As Stream
            Dim response As WebResponse = request.GetResponse()
            dataStream = response.GetResponseStream()
            Dim reader As New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()
            reader.Close()
            dataStream.Close()
            response.Close()

            Debug.Print("Response: " + responseFromServer)

            Return responseFromServer
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    ''' <summary>
    ''' Gibt Anzahl ungelesener Nachrichten zurück
    ''' </summary>
    ''' <param name="Username">GAYS.DE Benutzername</param>
    ''' <param name="Password">GAYS.DE Password</param>
    ''' <returns>Anzahl ungelesener Nachrichten, oder -1 für falsche Zugangsdaten</returns>
    ''' <remarks></remarks>
    Private Function httpGetMessageCountSimple(ByVal Username As String, ByVal Password As String) As Integer
        Dim queryString As String = "http://www.gays.de/api/messageCount/nickname/" + Username + "/password/" + Password
        Dim result As Integer = -1

        Try
            result = CInt(simpleHttpGetRequest(queryString))
        Catch ex As Exception
            ' nichts tun, Standard ist erstmal -1
        End Try

        Return result
    End Function

    ''' <summary>
    ''' Prüft, ob Zugangsdaten korrekt sind
    ''' </summary>
    ''' <param name="Username">GAYS.DE Benutzername</param>
    ''' <param name="Password">GAYS.DE Password</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function checkCredentials(ByVal Username As String, ByVal Password As String) As Boolean
        Return httpGetMessageCountSimple(Username, Password) >= 0
    End Function

    ''' <summary>
    ''' Gibt Anzahl ungelsener Nachrichten zurück
    ''' </summary>
    ''' <param name="Username">GAYS.DE Benutzername</param>
    ''' <param name="Password">GAYS.DE Password</param>
    ''' <returns>Anzahl ungelesener Nachrichten</returns>
    ''' <remarks></remarks>
    Public Function getMessageCount(ByVal Username As String, ByVal Password As String) As Integer
        Return httpGetMessageCountSimple(Username, Password)
    End Function

    Public Sub setOnlineStatus(ByVal Username As String, ByVal Password As String, ByVal Status As Integer)
        Dim queryString As String = "http://www.gays2.de/api/messageCount/nickname/" + Username + "/password/" + Password + "/status/" + Status.ToString

        Try
            simpleHttpGetRequest(queryString)
        Catch ex As Exception
            ' nichts tun, Standard ist erstmal -1
        End Try

    End Sub

End Class
